<template>
  <el-card>
    <template #header>个人中心</template>
    <div>
      <ul>
        <li>
          <span>头像：</span>
          <p class="avatar"><img :src="users.img_url" alt="" /></p>
          <el-upload
            class="avatar-uploader"
            action="http://wxopen.besthuishenghuo.com/api/file/pictureUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <span>账号：</span>
          <p>{{ users.username }}</p>
        </li>
        <li>
          <span>昵称：</span>
          <p>{{ users.nickname }}</p>
        </li>
        <li>
          <span>用户ID：</span>
          <p>{{ users.member_id }}</p>
        </li>
        <li>
          <span>创建时间：</span>
          <p>{{ users.create_time }}</p>
        </li>
        <li class="save">
          <el-button type="primary" @click="saveAvatar"> 保存 </el-button>
        </li>
      </ul>
    </div>
  </el-card>
</template>

<script>
import { avatarSave } from '../../api/users.js'
export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      avatarUrl: ''
    }
  },
  created() {
    this.users = JSON.parse(localStorage.getItem('users'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // 处理前端本地预览功能，而不是上传了图片
      this.imageUrl = URL.createObjectURL(file.raw)

      // 把响应回来的内容，主要是后端要求的数据存储起来，方便下一次用
      this.avatarUrl = res.data.id
      // 响应回来的图片路径
      this.img_url = res.data.img_url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // async getmyList() {
    //   //   const users = JSON.parse(localStorage.getItem('users'))
    //   const { data } = await peraonal({
    //     member_id: this.users.member_id,
    //     access_token: this.users.access_token
    //   })
    //   //   if (code === 1) {
    //   //     this.$message.success(msg)
    //   //   } else {
    //   //     this.$message.error(msg)
    //   //   }
    //   console.log(data)
    //   //   this.avatarUrl = data.head_img_id
    // },
    async saveAvatar() {
      if (this.avatarUrl === '') return ''
      const { code, msg } = await avatarSave({
        member_id: this.users.member_id,
        access_token: this.users.access_token,
        head_img_id: this.avatarUrl
      })
      //   const data = peraonal({
      //       access_token: this.users.access_token
      //   })
      if (code === 1) {
        this.$message.success(msg)
        // this.head_img_id = this.avatarUrl
        // console.log(this.img_url)
        this.users.img_url = this.imageUrl
        // console.log(this.users.img_url)

        // console.log(data)
        localStorage.setItem('users', JSON.stringify(this.users))
        location.reload()
        // localStorage.setItem('this.head_img_id', this.imageUrl)

        // console.log(this.imageUrl)
        // console.log(this.head_img_id)
        // console.log(this.imageUrl)
        // location.reload()
        // this.getmyList()
        // location.reload()
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  height: 100%;
  border: 0;
  ul {
    margin: 0;
    padding: 20px;
  }
  li {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    &.save {
      width: 100px;
      padding-left: 80px;
    }
    span {
      width: 80px;
      text-align: right;
      margin-right: 20px;
    }
    .avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
      background-color: #fff;
      border: 2px solid #f8f8f8;
    }
    img {
      width: 100%;
      transform: scale(1.5);
    }
  }
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 40px;
  }
  .avatar-uploader :hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    line-height: 78px;
    text-align: center;
  }
  .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
}
</style>
